package com.blog.controller.sys.menu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.common.base.BaseController;
import com.blog.common.base.ResponseReslut;
import com.blog.entity.sys.Menu;
import com.blog.entity.sys.RoleAndMenu;
import com.blog.service.sys.MenuService;
import com.blog.service.sys.RoleAndMenuService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RoleAndMenuService roleAndMenuService;
	
	@GetMapping("/list")
	@ResponseBody
	public List<Menu> listMenu(@RequestParam Map<String, Object> prams)
	{
		List<Menu> listMenu=menuService.listAllMenu(prams);
		return listMenu;
	}
	
	
	//打开 添加菜单 
	@GetMapping("/add/{parentId}")
	public ModelAndView add(@PathVariable("parentId") Integer parentId) {
		Map<String, Object> map=new HashMap<>();
		map.put("parentId", parentId);
		if (parentId==1) {
			map.put("pName", "根目录");
		} else {
			//如果不是根节点    查询菜单id查询 数据 返回给页面    parentId 其实就是menuId 
			map.put("pName",menuService.getMenuById(parentId).getName());
		}
		return view("/sys/menu/add",map);
	}
	
	//保存
	@PostMapping("/save")
	@ResponseBody
	public ResponseReslut saveMenu(Menu m)
	{
		//修改父id的 leaf状态   如果 父id的本身 就为leaf状态 就不改变
		if(StringUtils.isEmpty(m.getLeaf()))
		{
			return resultError("添加失败，请选择菜单类型！");
		}
		if(1!=m.getLeaf())
		{
			Menu mParent=new Menu();
			mParent.setId(m.getParentId());
			mParent.setLeaf(1);//1为目录
			menuService.updateMenu(mParent);
		}
		
		
		if(menuService.saveMenu(m)>0)
		{
			return setResultSuccess("添加成功");
		}	 
		
		return resultError("添加失败，请联系管理员");
	}
	
	
	//打开 修改菜单 
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Integer id) {
		Menu menuDo=menuService.getMenuById(id);
		Integer pId=menuDo.getParentId();
		Map<String, Object> map=new HashMap<>();
		if (pId==1) {
			map.put("pName", "根目录");
		} else {
			//如果不是根节点    查询菜单id查询 数据 返回给页面    parentId 其实就是menuId 
			map.put("pName",menuService.getMenuById(pId).getName());
		}
		map.put("menuDo", menuDo);
		return view("/sys/menu/edit",map);
	}
	
	
	//保存
	@PostMapping("/update")
	@ResponseBody
	public ResponseReslut updateMenu(Menu m)
	{
		if(menuService.updateMenu(m)>0)
		{
			return setResultSuccess("修改成功");
		}	 
		
		return resultError("修改失败，请联系管理员");
	}
	
	
	@PostMapping("/remove")
	@ResponseBody
	public ResponseReslut removeMenu(Integer id)
	{
		//软删除
		//	    Menu m=new Menu();
		//		m.setId(id); 
		//		m.setState(0);//0表示  停用
		//		menuService.updateMenu(m)
		
		//查询是否有下级，如果有 就不能删除
		Map<String,Object> map=new HashMap<>();
		map.put("parentId", id);
		List<Menu> listMenu=menuService.listAllMenu(map);
		if(listMenu.size()>0)
		{
			return resultError("删除失败,请先把子节点删除！");
		}
		
		Integer menuId=id;
		RoleAndMenu rm=new RoleAndMenu();
		rm.setMenu_id(menuId);
		roleAndMenuService.deleteRoleMenu(rm);
		if(menuService.delectMenuById(menuId)>0)
		{
			return setResultSuccess("删除成功");
		}	 
		
		return resultError("删除失败，请联系管理员");
	}
	
}
